package com.jqsoft.launcher.activity.gairui;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.jqsoft.launcher.activity.JQActivity;
import com.jqsoft.launcher.R;
import com.garea.medical.MedicalFactory;
import com.garea.medical.glu.IGlu;
import com.garea.medical.glu.IGlu.OnGluListener;
import com.garea.medical.glu.IGluData;
import com.garea.medical.glu.IGluState;

import org.json.JSONObject;

import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;

/**
 * 测量血糖
 * @作者：周桃有
 * @日期：2015-7-8
 */
public class GluExample extends JQActivity {
	
	private IGlu mGlu;
	
	private TextView tvCN, tvState, tvCD, tvValue, tvSpec, tvTitle;
	private Button btnCN;
	private Button btnSave;
	private String gGLU="";
	@Override
	protected int getContentLayout() {
		
		return R.layout.example_glu;
	}

	@Override
	protected void initGui() {
		btnCN = (Button) this.findViewById(R.id.btnSetCN);
		tvCN = (TextView) this.findViewById(R.id.tvCN);
		tvState = (TextView) this.findViewById(R.id.tvBloodState);
		tvCD = (TextView) this.findViewById(R.id.tvBloodCD);
		tvValue = (TextView) this.findViewById(R.id.tvBloodValue);
		tvSpec = (TextView) this.findViewById(R.id.tvSpecName);
		tvTitle = (TextView) this.findViewById(R.id.tvSpecTitle);
		btnSave=(Button) findViewById(R.id.btnSave);
	}

	@Override
	protected void initAction() {
		setGoBack();
		btnCN.setEnabled(false);
		btnCN.setOnClickListener(new OnClickListener() {
			/**
			 * 弹出一个滑轮对话框来设置校验码
			 */
			@Override
			public void onClick(View v) {
				final WheelView cnView = new WheelView(GluExample.this);
				cnView.setResourceContext(GluExample.this);
				cnView.setVisibleItems(3);
				cnView.setCyclic(true);
				String[] str = new String[36];
				for (int i = 0; i < str.length; i++) {
					str[i] = "C" + i;
				}
				
				cnView.setViewAdapter(new ArrayWheelAdapter<String>(GluExample.this, str));
				String cCN = tvCN.getText().toString();
				if (!tvCN.getText().toString().contains("-")) {
					cnView.setCurrentItem(Integer.parseInt(cCN.replace("C", "")));
				}
				
				final AlertDialog diag = new AlertDialog.Builder(GluExample.this).create();
				
				diag.setTitle("校验码");
				diag.setView(cnView);
				diag.setButton("确定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						mGlu.setCheckNumber((byte) cnView.getCurrentItem());
						diag.dismiss();
					}
				});
				diag.setButton2("取消", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						diag.dismiss();
					}
				});
				diag.show();
				diag.getWindow().setLayout(300, 400);
				
			}
		});
		
	btnSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				JSONObject jsonObject = new JSONObject();
				try {
					jsonObject.put("gGLU", gGLU);//血糖
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//设置当前activity被关闭的时候的结果数据
				Intent data = new Intent();
				data.putExtra("jsonstr", jsonObject.toString());
				setResult(5, data);
				
				//关闭当前的activity
				GluExample.this.finish();
			}
			
		});
	}

	@Override
	protected void initData() {
		setTitle("测量血糖");
		tvTitle.setText("血糖");
		tvSpec.setText("血糖:");
		
		initGlu();
	}
	
	
	private void initGlu() {
		mGlu = MedicalFactory.createLocalGlu();
		mGlu.setListener(new OnGluListener() {

			/**
			 * 校验码被更改
			 */
			@Override
			public void onCheckNumberChanged(byte cn) {
				//校验码更改
				tvCN.setText("C" + Integer.toString(cn));
			}

			/**
			 * 获取到当前校验码
			 */
			@Override
			public void onCurrentCheckNumber(byte cn) {
				//获取到当前校验码
				tvCN.setText("C" + Integer.toString(cn));
				btnCN.setEnabled(true);
			}

			/**
			 * 接收血糖数据
			 */
			@Override
			public void onReceivedData(IGluData d) {
				//接收血糖数据
				if (d.isValid()) {
					gGLU=Float.toString(d.getValue());
					tvValue.setText(gGLU);
				} else {
					tvValue.setText("---");
				}
			}

			@Override
			public void onReceivedState(IGluState s) {
				//接收血糖状态
				String msg = null;
				int step = 0;
				
				if (s.getError() == 8) {
					Toast.makeText(GluExample.this, "请拔出试条重新进入血糖界面",
							Toast.LENGTH_SHORT).show();
					return;
				}
				
				switch (s.getState()) {
				case IGluState.STATE_IDEL:
					tvCD.setText("--");
					step = 0;
					msg = "空闲";
					break;
				case IGluState.STATE_INSERTED:
					tvCD.setText("--");
					tvValue.setText("---");
					msg = "试条已插入";
					step = 1;
					break;

				case IGluState.STATE_MEASURE:
					msg = "开始测量";
					step = 2;
					if (s.getCountDown() >= 0) { 
						tvCD.setText(Byte.toString(s.getCountDown()));
					}
					break;

				case IGluState.STATE_COMPLETE:
					step = 3;
					msg = "测量结束";
					break;

				default:
					break;
				}
				

				if (msg != null) {
					if (s.getState() == IGluState.STATE_INSERTED
							&& s.getError() == IGluState.ERR_USED) {
						tvState.setText("试条已过期");
						tvState.setTextColor(Color.RED);
//						panel.setStep(0);
					} else {
						tvState.setText(msg);
						tvState.setTextColor(Color.BLACK);
//						panel.setStep(step);
					}
				}
//				currState = s.getState();
			}
			
			@Override
			public void onInited() {
				mGlu.start();
			}
			
			@Override
			public void onStarted() {
				/**
				 * 获取校验码
				 */
				mGlu.getCheckNumber();
			}
		});
		mGlu.init();
	}
	
	@Override
	public void onStop() {
		super.onStop();
		mGlu.stop();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		mGlu.uninit();
	}

}
